
//

#import "NSString+Date.h"

@implementation NSString (Date)

+ (NSString *)getDateTimeInterval:(NSTimeInterval)time Format:(NSString *)format isMS:(BOOL)ms
{
    if (ms) {
        time = time/1000;
    }
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle: NSDateFormatterMediumStyle];
    [formatter setTimeStyle: NSDateFormatterShortStyle];
    [formatter setDateFormat: format]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制v1296035591 为秒
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970: time];
    return [formatter stringFromDate:confromTimesp];
}
- (NSString*)weekDayStr:(NSString *)format
{
    NSString *weekDayStr = nil;
    
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    
    NSString *str = [self description];
    if (str.length >= 10) {
        NSString *nowString = [str substringToIndex:10];
        NSArray *array = [nowString componentsSeparatedByString:@"-"];
        if (array.count == 0) {
            array = [nowString componentsSeparatedByString:@"/"];
        }
        if (array.count >= 3) {
            NSInteger year = [[array objectAtIndex:0] integerValue];
            NSInteger month = [[array objectAtIndex:1] integerValue];
            NSInteger day = [[array objectAtIndex:2] integerValue];
            [comps setYear:year];
            [comps setMonth:month];
            [comps setDay:day];
        }
    }
    
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate *_date = [gregorian dateFromComponents:comps];
    NSDateComponents *weekdayComponents = [gregorian components:NSCalendarUnitWeekday fromDate:_date];
    NSInteger week = [weekdayComponents weekday];
    //    week ++;
    
    NSArray *languages = [NSLocale preferredLanguages];
    NSString *currentLanguage = [languages objectAtIndex:0];
    BOOL china = [currentLanguage  containsString: @"Hans"];
    switch (week) {
        case 1:
            weekDayStr = china?@"周日":@"Sunday";
            break;
        case 2:
            weekDayStr = china?@"周一":@"Monday";
            break;
        case 3:
            weekDayStr = china?@"周二":@"Tuesday";
            break;
        case 4:
            weekDayStr = china?@"周三":@"Wednesday";
            break;
        case 5:
            weekDayStr = china?@"周四":@"Thursday";
            break;
        case 6:
            weekDayStr = china?@"周五":@"Friday";
            break;
        case 7:
            weekDayStr = china?@"周六":@"Saturday";
            break;
        default:
            weekDayStr = @"";
            break;
    }
    return weekDayStr;
}
//返回星期几数字
- (NSInteger)weekDayInteger:(NSString *)format
{
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    
    NSString *str = [self description];
    if (str.length >= 10) {
        NSString *nowString = [str substringToIndex:10];
        NSArray *array = [nowString componentsSeparatedByString:@"-"];
        if (array.count == 0) {
            array = [nowString componentsSeparatedByString:@"/"];
        }
        if (array.count >= 3) {
            NSInteger year = [[array objectAtIndex:0] integerValue];
            NSInteger month = [[array objectAtIndex:1] integerValue];
            NSInteger day = [[array objectAtIndex:2] integerValue];
            [comps setYear:year];
            [comps setMonth:month];
            [comps setDay:day];
        }
    }
    
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate *_date = [gregorian dateFromComponents:comps];
    NSDateComponents *weekdayComponents = [gregorian components:NSCalendarUnitWeekday fromDate:_date];
    NSInteger week = [weekdayComponents weekday];
    week = week - 1;
    if (week == 0) {
        week = 7;
    }
    return week;
}
@end
